// 处理 JSON 为后端需要的样子
function handleJson (jsonObj) {
  const str = JSON.stringify(jsonObj)
  return '『' + str.slice(1, str.length - 1) + '』'
}

const date = new Date()
const thisYear = date.getFullYear()
const thisMonth = date.getMonth() + 1
const thisDay = date.getDate()

export const getHistoryTaskConfig = selectMonth => {
  // 进行日期筛选
  const startTime = new Date(thisYear, selectMonth - 1, 1, 0, 0, 0)
  let endTime = new Date(thisYear, selectMonth - 1, 0, 23, 59, 59)

  if (selectMonth === thisMonth) {
    // 当筛选月等于当前月，取今天
    endTime = new Date(thisYear, selectMonth - 1, thisDay, 23, 59, 59)
  }

  const typeMap = ['repair', 'maintain', 'inspection', 'patrol']

  const getData = (type) => ({
    type: 'taskNum',
    keyList: [type],
    timeBefore: endTime.getTime(), // 结束之前
    timeAfter: startTime.getTime(), // 开始时间
    segmentation: 31,
    formatType: 3
  })

  const params = typeMap.reduce((acc, item) => {
    acc[item] = '!' + handleJson(getData(item))
    return acc
  }, {})

  return params
}

export const getAchievementsConfig = (selectMonth) => {
  // 进行日期筛选
  const startTime = new Date(thisYear, selectMonth - 1, 1, 0, 0, 0)
  let endTime = new Date(thisYear, selectMonth - 1, 0, 23, 59, 59)

  if (selectMonth === thisMonth) {
    // 当筛选月等于当前月，取今天
    endTime = new Date(thisYear, selectMonth - 1, thisDay, 23, 59, 59)
  }

  const typeMap = ['repair', 'maintain', 'inspection', 'patrol']

  const getData = (type) => ({
    type: 'taskNum',
    keyList: [type],
    timeBefore: endTime.getTime(), // 结束之前
    timeAfter: startTime.getTime(), // 开始时间
    segmentation: 11,
    formatType: 2,
    keyName: 'userName',
    valueName: 'num'
  })

  const params = typeMap.reduce((acc, item) => {
    acc[item] = '!' + handleJson(getData(item))
    return acc
  }, {})

  return params
}
